package com.hmy.finance.service.receipt.impl;

import com.hmy.ccp.error.exception.HMYRuntimeException;
import com.hmy.finance.dal.mapper.ContractDailyBillMapper;
import com.hmy.finance.dal.mapper.DeviceDailyBillMapper;
import com.hmy.finance.dal.mapper.RechargeTransactionBillMapper;
import com.hmy.finance.dal.po.RechargeTransactionBillPo;
import com.hmy.finance.service.receipt.IncomeExpensesService;
import com.hmy.finance.service.receipt.RechargeBalanceAmountService;
import com.hmy.finance.service.receipt.dto.recharge.RechargeContractFeeBillDto;
import com.hmy.finance.service.receipt.dto.recharge.RechargeTransactionBillDto;
import com.hmy.finance.service.receipt.rdto.TradeRdto;
import com.hmy.finance.service.receipt.rdto.recharge.RechargeContractFeeBillRdto;
import com.hmy.finance.service.receipt.rdto.recharge.RechargeTransactionBillRdto;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 余额回充逻辑处理
 *
 * @author songkf
 */
@Service
public class RechargeBalanceAmountServiceImpl implements RechargeBalanceAmountService {

    @Resource
    private RechargeTransactionBillMapper rechargeTransactionBillMapper;

    @Resource
    private ContractDailyBillMapper contractDailyBillMapper;

    @Resource
    private DeviceDailyBillMapper deviceDailyBillMapper;
    @Resource
    private IncomeExpensesService incomeExpensesService;

    @Override
    public RechargeContractFeeBillRdto rechargeContractFeeBill(RechargeContractFeeBillDto rechargeContractFeeBillDto) {
        //查询该合同下所有待回充的回充业务单据
        rechargeContractFeeBillDto.setRechargeTransactionBillList(rechargeTransactionBillMapper.selectList(rechargeContractFeeBillDto.getContractWaitRechargeBillListQueryWrapper()));
        //遍历所有回充业务单据
        List<RechargeContractFeeBillRdto.RechargeResult> rechargeResultList = new ArrayList<>(rechargeContractFeeBillDto.getRechargeTransactionBillList().size());
        for (RechargeTransactionBillPo rechargeTransactionBillPo : rechargeContractFeeBillDto.getRechargeTransactionBillList()) {
            RechargeContractFeeBillRdto.RechargeResult rechargeResult = new RechargeContractFeeBillRdto.RechargeResult();
            RechargeTransactionBillRdto rdto = this.rechargeTransactionBillAmount(RechargeTransactionBillDto.getInstance(rechargeTransactionBillPo));
            if (rdto != null) {
                rechargeResult.setRechargeTransactionBillId(rdto.getRechargeTransactionBillId());
                rechargeResult.setStatus(rdto.getRechargeResult());
            } else {
                rechargeResult.setRechargeTransactionBillId(rechargeTransactionBillPo.getBillId());
                rechargeResult.setStatus(false);
            }
            rechargeResultList.add(rechargeResult);
        }
        //构建响应参数
        RechargeContractFeeBillRdto rechargeContractFeeBillRdto = new RechargeContractFeeBillRdto();
        rechargeContractFeeBillRdto.setContractId(rechargeContractFeeBillDto.getContractId());
        rechargeContractFeeBillRdto.setRechargeTransactionBillResultList(rechargeResultList);
        return rechargeContractFeeBillRdto;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RechargeTransactionBillRdto rechargeTransactionBillAmount(RechargeTransactionBillDto rechargeTransactionBillDto) {
        //传递的业务单据数据不能为空
        if (rechargeTransactionBillDto.getRechargeTransactionBillId() == null && rechargeTransactionBillDto.getRechargeTransactionBill() == null) {
            throw new HMYRuntimeException(506720001);
        }
        if (rechargeTransactionBillDto.getRechargeTransactionBill() == null) {
            rechargeTransactionBillDto.setRechargeTransactionBill(rechargeTransactionBillMapper.selectById(rechargeTransactionBillDto.getRechargeTransactionBillId()));
            //传递的业务单据数据不能不存在
            if (rechargeTransactionBillDto.getRechargeTransactionBill() == null) {
                throw new HMYRuntimeException(506720001);
            }
        }
        //构建联系人账户入账DTO，并执行联系人账户入账
        TradeRdto tradeRdto = incomeExpensesService.incomeBalance(rechargeTransactionBillDto.getContactorIncomeDto());
        //如果联系人账户入账成功则将回充业务单据状态修改为已处理
        if (tradeRdto.getResult()) {
            rechargeTransactionBillDto.getRechargeTransactionBill().setRechargeStatus(3);
            return rechargeTransactionBillDto.getRdto(rechargeTransactionBillMapper.updateById(rechargeTransactionBillDto.getRechargeTransactionBill()) > 0);
        }
        //默认处理失败
        return rechargeTransactionBillDto.getRdto(Boolean.FALSE);
    }
}
